package com.eveinfo.display.planner;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;

import com.eveinfo.display.SheetView;
import com.eveinfo.tools.ActivityLauncher;

public class PlannerAdapter extends BaseAdapter {

	private final Context context;
	private final Planner planner;
	protected final int characterID;

	public PlannerAdapter(Context context, ListView listView, Planner planner, int characterID) {
		this.context = context;
		this.planner = planner;
		this.characterID = characterID;
		listView.setAdapter(this);
	}

	@Override
	public int getCount() {
		return planner.getSize();
	}

	@Override
	public Object getItem(int position) {
		return planner.getTask(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		TaskPlannerRenderer renderer = new TaskPlannerRenderer(context);
		final TaskPlanner task = planner.getTask(position);
		renderer.setTask(task);
		renderer.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				ActivityLauncher.displaySkill(context, task.getSkillID(), characterID);
			}
		});
		renderer.setOnLongClickListener(new OnLongClickListener() {
			@Override
			public boolean onLongClick(View v) {
				SheetView.getPlannerManager().removeSkillFromPanner(planner.getName(), task.getId());
				notifyDataSetChanged();
				return true;
			}
		});
		return renderer;
	}
}
